﻿using System.Collections.Generic;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using NetProtocol.Client;

namespace IQIGame.Onigao.GamePlay
{
    public class GmType
    {
        public int type;
        public string typeName;
        public int parentType;
        public List<GmType> subTypes;
    }

    /// <summary>
    /// 类名 : 协议 - 游戏GM命令
    /// 作者 : Canyon / 龚阳辉
    /// 日期 : 2024-05-14 18:49
    /// 功能 : 
    /// </summary>
    public class GameCmdModule : Singleton<GameCmdModule>, CNetGameCmd
    {
        private GameCmdModule() { }

        public void InitModule()
        {
            this.InitGmTypes();
        }

        public void ClearModule()
        {
            this.dicMainType.Clear();
            this.listMainTypes.Clear();
        }

        #region 网络
        public static CNetGameCmd NetCore => Instance;

        ISession CNetGameCmd.GetSession()
        {
            return GameServerModule.Instance.CurrSession;
        }
        #endregion

        Dictionary<int, GmType> dicMainType = new();
        List<int> listMainTypes = new();

        void InitGmTypes()
        {
            if (listMainTypes.Count > 0)
                return;

            System.Func<List<GmType>, int, bool> _funcIsInList = (list, subType) =>
            {
                foreach (var item in list)
                {
                    if (item.type == subType)
                        return true;
                }
                return false;
            };

            var list = TableCenter.gmCmd.DataList;
            foreach (var item in list)
            {
                if (!this.dicMainType.TryGetValue(item.Type, out GmType type))
                {
                    type = new GmType();
                    type.type = item.Type;
                    type.typeName = item.TypeName;
                    type.subTypes = new();

                    this.dicMainType[item.Type] = type;
                    this.listMainTypes.Add(item.Type);
                }

                // 筛选子类型
                if (_funcIsInList(type.subTypes, item.SubType))
                {
                    continue;
                }

                type.subTypes.Add(new GmType()
                {
                    parentType = item.Type,
                    type = item.SubType,
                    typeName = item.SubTypeName,
                });
            }
        }

        public List<int> MainTypes
        {
            get
            {
                this.InitGmTypes();
                return this.listMainTypes;
            }
        }

        public GmType GetMainGmType(int mainType)
        {
            return this.dicMainType.TryGetValue(mainType, out GmType type) ? type : null;
        }
    }
}
